package com.tiancheng.trade.authserver.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tiancheng.trade.authserver.entity.AuthApplication;
import com.tiancheng.trade.authserver.entity.AuthRole;
import com.tiancheng.trade.authserver.mapper.AuthApplicationMapper;
import com.tiancheng.trade.authserver.mapper.AuthRoleMapper;
import com.tiancheng.trade.authserver.model.reponse.SubjectAllRoleResult;
import com.tiancheng.trade.authserver.service.IAuthRoleService;
import jakarta.annotation.Resource;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * @Author: likailun
 * @Description: 授权角色servie实现
 * @Date: create in 2024/10/21 17:21
 */
@Service
public class AuthRoleServiceImpl extends ServiceImpl<AuthRoleMapper, AuthRole> implements IAuthRoleService {

    @Resource
    private AuthApplicationMapper authApplicationMapper;

    @Override
    public List<AuthRole> selectUserRolesByApplicationClientId(Long userId, String clientId) {
        final List<AuthApplication> authApplications = authApplicationMapper.selectList(new LambdaQueryWrapper<AuthApplication>().eq(AuthApplication::getClientId, clientId));
        if(CollectionUtils.isNotEmpty(authApplications)){
            return this.baseMapper.queryByApplicationIdAndUserId(userId, authApplications.get(0).getId());
        }
        return Collections.emptyList();
    }

    @Override
    public Map<Long, AuthRole> getByIds(Set<Long> roleIds) {
        return list(new LambdaQueryWrapper<AuthRole>().in(AuthRole::getId,roleIds)).stream().collect(Collectors.toMap(AuthRole::getId, Function.identity()));
    }

    @Override
    public void updateDataPermissionCode(AuthApplication application) {
        final AuthRole authRole = new AuthRole();
        authRole.setDataPermissionCode(application.getDataPermissionCode());
        this.update(authRole,new LambdaQueryWrapper<AuthRole>().eq(AuthRole::getApplicationId,application.getId()));
    }

    @Override
    public SubjectAllRoleResult getMyAllRoles() {
//        Object loginId = StpKit.DEFAULT.getLoginId();
//        String clientName = sessionCommonProfile.getClientName();
//        log.info("获取请对象类似:{}, id:{}所有角色信息", clientName, sessionCommonProfile.getId());
//
//        RoleQuery query = new RoleQuery();
//        query.setLimit(null);
//        if (clientName.contains("user") || clientName.contains("oauth2")) {
//            query.setUserMapRole(new UserMapRoleQuery());
//            query.getUserMapRole().setUserId(new StringParam(sessionCommonProfile.getId()));
//        } else {
//            query.setApplicationRole(new ApplicationMapRoleQuery());
//            query.getApplicationRole().setApplicationId(new StringParam(sessionCommonProfile.getId()));
//        }
//        this.list(new LambdaQueryWrapper<AuthRole>().eq(AuthRole::get));
//        List<Role> roles = this.getByQueryParam(query);
//        Map<String, Application> idMapApplication = this.applicationService.getByIds(roles.stream().map(Role::getApplicationId).collect(Collectors.toSet()));
//        SubjectAllRoleResult result = new SubjectAllRoleResult();
//        result.setAllRoleCodes(new HashSet<>());
//        result.setClientIdMapAllRoleCodes(new HashMap<>());
//        for (Role role : roles) {
//            String clientId = idMapApplication.get(role.getApplicationId()).getClientId();
//            result.getAllRoleCodes().add(role.getCode());
//            if (!result.getClientIdMapAllRoleCodes().containsKey(clientId)) {
//                result.getClientIdMapAllRoleCodes().put(clientId, new HashSet<>());
//            }
//            result.getClientIdMapAllRoleCodes().get(clientId).add(role.getCode());
//        }
//        return result;
        return null;
    }
}
